<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付状态" prop="payStatus">
            <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable size="small">
              <el-option v-for="dict in payStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付开始时间" prop="beginTime">
            <el-date-picker clearable size="small" v-model="queryParams.beginTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择支付开始时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付结束时间" prop="endTime">
            <el-date-picker clearable size="small" v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择支付结束时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付时间" prop="payTime">
            <el-date-picker clearable size="small" v-model="queryParams.payTime" type="date" value-format="yyyy-MM-dd"
              placeholder="选择支付时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付方式" prop="payMethod">
            <el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable size="small">
              <el-option v-for="dict in payMethodOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="付款业务编号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" placeholder="请输入付款业务编号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单类型" prop="orderType">
            <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable size="small">
              <el-option v-for="dict in orderTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付账户" prop="payAccount">
            <el-input v-model="queryParams.payAccount" placeholder="请输入支付账户" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="第三方支付流水号" prop="payRefSn">
            <el-input v-model="queryParams.payRefSn" placeholder="请输入第三方支付流水号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker clearable size="small" v-model="queryParams.createTime" type="date"
              value-format="yyyy-MM-dd" placeholder="选择创建时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="更新时间" prop="updateTime">
            <el-date-picker clearable size="small" v-model="queryParams.updateTime" type="date"
              value-format="yyyy-MM-dd" placeholder="选择更新时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="IP地址" prop="ip">
            <el-input v-model="queryParams.ip" placeholder="请输入IP地址" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="openid" prop="openid">
            <el-input v-model="queryParams.openid" placeholder="请输入openid" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提交来源" prop="platSource">
            <el-select v-model="queryParams.platSource" placeholder="请选择提交来源" clearable size="small">
              <el-option v-for="dict in platSourceOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item style="width: 100%;display: flex; align-items: center; justify-content: center;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['order:payInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['order:payInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['order:payInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table v-loading="loading" :data="payInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="付款流水ID" align="center" prop="payUid" sortable />
        <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true" />
        <el-table-column label="支付状态" align="center" prop="payStatus" :formatter="payStatusFormat" />
        <el-table-column label="支付开始时间" align="center" prop="beginTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付结束时间" align="center" prop="endTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="payTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center" prop="payMethod" :formatter="payMethodFormat" />
        <el-table-column label="付款业务编号" align="center" prop="orderNo" :show-overflow-tooltip="true" />
        <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat" />
        <el-table-column label="总支付金额" align="center" prop="payAmount" :show-overflow-tooltip="true" />
        <el-table-column label="实付金额" align="center" prop="realAmount" :show-overflow-tooltip="true" />
        <el-table-column label="货币单位" align="center" prop="currencyUnit" :formatter="currencyUnitFormat" />
        <el-table-column label="支付账户" align="center" prop="payAccount" :show-overflow-tooltip="true" />
        <el-table-column label="第三方支付流水号" align="center" prop="payRefSn" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="IP地址" align="center" prop="ip" :show-overflow-tooltip="true" />
        <el-table-column label="openid" align="center" prop="openid" :show-overflow-tooltip="true" />
        <el-table-column label="提交来源" align="center" prop="platSource" :formatter="platSourceFormat" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
              v-hasPermi="['order:payInfo:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['order:payInfo:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['order:payInfo:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改付款流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户UID" prop="customerUid">
          <el-input v-model="form.customerUid" placeholder="请输入客户UID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-select v-model="form.payStatus" placeholder="请选择支付状态">
            <el-option v-for="dict in payStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付开始时间" prop="beginTime">
          <el-date-picker clearable size="small" v-model="form.beginTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择支付开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付结束时间" prop="endTime">
          <el-date-picker clearable size="small" v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择支付结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable size="small" v-model="form.payTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-select v-model="form.payMethod" placeholder="请选择支付方式">
            <el-option v-for="dict in payMethodOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款业务编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入付款业务编号" />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择订单类型">
            <el-option v-for="dict in orderTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总支付金额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入总支付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="realAmount">
          <el-input v-model="form.realAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="货币单位" prop="currencyUnit">
          <el-select v-model="form.currencyUnit" placeholder="请选择货币单位">
            <el-option v-for="dict in currencyUnitOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付账户" prop="payAccount">
          <el-input v-model="form.payAccount" placeholder="请输入支付账户" />
        </el-form-item>
        <el-form-item label="第三方支付流水号" prop="payRefSn">
          <el-input v-model="form.payRefSn" placeholder="请输入第三方支付流水号" />
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="提交来源" prop="platSource">
          <el-select v-model="form.platSource" placeholder="请选择提交来源">
            <el-option v-for="dict in platSourceOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看付款流水对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPayInfo,
    getPayInfo,
    delPayInfo,
    addPayInfo,
    updatePayInfo
  } from "@/api/order/payInfo";

  export default {
    name: "PayInfo",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 付款流水表格数据
        payInfoList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 支付状态字典
        payStatusOptions: [],
        // 支付方式字典
        payMethodOptions: [],
        // 订单类型字典
        orderTypeOptions: [],
        // 货币单位字典
        currencyUnitOptions: [],
        // 提交来源字典
        platSourceOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerName: null,
          payStatus: null,
          beginTime: null,
          endTime: null,
          payTime: null,
          payMethod: null,
          orderNo: null,
          orderType: null,
          payAccount: null,
          payRefSn: null,
          createTime: null,
          updateTime: null,
          ip: null,
          openid: null,
          platSource: null
        },
        columns: [{
            label: '付款流水ID',
            dataIndex: 'payUid',
          },
          {
            label: '客户UID',
            dataIndex: 'customerUid',
          },
          {
            label: '客户名称',
            dataIndex: 'customerName',
          },
          {
            label: '支付状态',
            dataIndex: 'payStatus',
          },
          {
            label: '支付开始时间',
            dataIndex: 'beginTime',
          },
          {
            label: '支付结束时间',
            dataIndex: 'endTime',
          },
          {
            label: '支付时间',
            dataIndex: 'payTime',
          },
          {
            label: '支付方式',
            dataIndex: 'payMethod',
          },
          {
            label: '付款业务编号',
            dataIndex: 'orderNo',
          },
          {
            label: '订单类型',
            dataIndex: 'orderType',
          },
          {
            label: '总支付金额',
            dataIndex: 'payAmount',
          },
          {
            label: '实付金额',
            dataIndex: 'realAmount',
          },
          {
            label: '货币单位',
            dataIndex: 'currencyUnit',
          },
          {
            label: '支付账户',
            dataIndex: 'payAccount',
          },
          {
            label: '第三方支付流水号',
            dataIndex: 'payRefSn',
          },
          {
            label: '创建时间',
            dataIndex: 'createTime',
          },
          {
            label: '更新时间',
            dataIndex: 'updateTime',
          },
          {
            label: 'IP地址',
            dataIndex: 'ip',
          },
          {
            label: 'openid',
            dataIndex: 'openid',
          },
          {
            label: '提交来源',
            dataIndex: 'platSource',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("order_pay_status").then(response => {
        this.payStatusOptions = response.data;
      });
      this.getDicts("order_pay_method").then(response => {
        this.payMethodOptions = response.data;
      });
      this.getDicts("order_order_type").then(response => {
        this.orderTypeOptions = response.data;
      });
      this.getDicts("order_currency_unit").then(response => {
        this.currencyUnitOptions = response.data;
      });
      this.getDicts("order_plat_source").then(response => {
        this.platSourceOptions = response.data;
      });
    },
    methods: {
      /** 查询付款流水列表 */
      getList() {
        this.loading = true;
        listPayInfo(this.queryParams).then(response => {
          this.payInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 支付状态字典翻译
      payStatusFormat(row, column) {
        return this.selectDictLabel(this.payStatusOptions, row.payStatus);
      },
      // 支付方式字典翻译
      payMethodFormat(row, column) {
        return this.selectDictLabel(this.payMethodOptions, row.payMethod);
      },
      // 订单类型字典翻译
      orderTypeFormat(row, column) {
        return this.selectDictLabel(this.orderTypeOptions, row.orderType);
      },
      // 货币单位字典翻译
      currencyUnitFormat(row, column) {
        return this.selectDictLabel(this.currencyUnitOptions, row.currencyUnit);
      },
      // 提交来源字典翻译
      platSourceFormat(row, column) {
        return this.selectDictLabel(this.platSourceOptions, row.platSource);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          payUid: null,
          customerUid: null,
          customerName: null,
          payStatus: null,
          beginTime: null,
          endTime: null,
          payTime: null,
          payMethod: null,
          orderNo: null,
          orderType: null,
          payAmount: null,
          realAmount: null,
          currencyUnit: null,
          payAccount: null,
          payRefSn: null,
          createTime: null,
          updateTime: null,
          ip: null,
          openid: null,
          platSource: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.payUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加付款流水";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看付款流水";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const payUid = row.payUid || this.ids
        getPayInfo(payUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改付款流水";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.payUid != null) {
              updatePayInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPayInfo(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const payUids = row.payUid || this.ids;
        this.$confirm('是否确认删除付款流水编号为"' + payUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPayInfo(payUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('order/payInfo/export', {
          ...this.queryParams
        }, `order_payInfo.xlsx`)
      }
    }
  };
</script>
